<div class="lsummary">
<ul class="summary">
	<li class='asummary_item'><a href="#all">Afficher toutes les étapes</a></li>
<?php 
foreach ($roots as $item) {
echo "  <li><a href=\"#".$item->key."\">".$item->label."</a></li>\n";
}
?>
</ul>
</div>

<div class="rsummary">
<div class="yiiForm">

<input type="hidden" name="project_id" id="project_id" value="<?php echo $_GET['id']; ?>" />

<?php

$this->widget('application.components.RequiredMessage');

if (isset($meeting) && $meeting->hasErrors()) {
	echo "Impossible de sauvegarder le premier rendez vous:";
	echo CHtml::errorSummary($meeting);
}

global $types; 
$types = array(ProjectTopic::STEP => "step", ProjectTopic::TOPIC => "topic");

echo CHtml::beginForm('', 'post', array (
	'id' => "steps"
));

//echo Yii::t('views', 'Topics available');

function display_topics($page, $model, $odd, $topics) {
	global $types;

	if ($topics == null || ! is_array($topics))
		return $odd;
		
	echo "<ul class=\"topic_tree\">";
	foreach ($topics as $topic) {
		echo "<li id=\"" . $topic->key . "\"><div class=\"item " . $types[$topic->item_type] . "_" . ($odd ? 'odd' : 'even') . "\">\n  ";
		echo CHtml::activeHiddenField($topic, "[".$topic->key."]id");
		if ($topic->children != null && count($topic->children) > 0) {
			echo CHtml::activeHiddenField($topic, "[".$topic->key."]checked", array('class'=>"cb_select_item"));
		} else {
			echo CHtml::activeCheckBox($topic, "[".$topic->key."]checked", array('class'=>"cb_select_item"));
		}
		if ($topic->parent_key != null && $topic->parent_key != "")
			echo CHtml::activeHiddenField($topic, "[".$topic->key."]parent_key");
		
		echo "  <span id=\"label_" . $topic->key . "\" class=\"label\"><a name=\"#".$topic->key."\">" . $topic->label . "</a></span>&nbsp;\n";
		echo "  <span id=\"user_inputs_" . $topic->key . "\" class=\"user_inputs\">\n    ";

		$attribute = "[".$topic->key."]start_date";
		$opts = array();
		CHtml::resolveNameId($topic, $attribute, $opts);
		$opts['size'] = 12;
		$opts['class'] = 'start_date';
		$page->widget('zii.widgets.jui.CJuiDatePicker', array (
			'name' => $opts['name'],
			'value' => ($topic->start_date != null ? $topic->start_date->format(Constants::DISPLAY_DATE_FORMAT) : DateUtils::today()->format(Constants::DISPLAY_DATE_FORMAT)),
			'options' => Constants::$JuiDatePickerOptions,
			'htmlOptions' => $opts
		));
		
		$attribute = "[".$topic->key."]end_date";
		$opts = array();
		CHtml::resolveNameId($topic, $attribute, $opts);
		$opts['size'] = 12;
		$opts['class'] = 'end_date';
		$page->widget('zii.widgets.jui.CJuiDatePicker', array (
			'name' => $opts['name'],
			'value' => ($topic->end_date != null ? $topic->end_date->format(Constants::DISPLAY_DATE_FORMAT) : DateUtils::today()->format(Constants::DISPLAY_DATE_FORMAT)),
			'options' => Constants::$JuiDatePickerOptions,
			'htmlOptions' => $opts
		));
		
		echo "\n  </span>\n";
		echo "  <div id=\"comment_" . $topic->key . "\" class=\"comment_area\">\n    ";
		
		echo "<div><a name=\"validation_comment_".$topic->key."_lnk\" alt=\"commentaire de validation\" class=\"validation_link\"></a>";
		$optscb = array('value' => ProjectTopic::YES, 'uncheckValue' => ProjectTopic::NO, 'class' => 'validation_cb adviser_validation');
		if ($topic->adviser_validation == ProjectTopic::INHERITS) {
			$optscb['value'] = ProjectTopic::INHERITS;
			$optscb['disabled'] = 'disabled';
		}
		echo CHtml::activeCheckBox($topic, "[".$topic->key."]adviser_validation", $optscb);
		echo CHtml::activeLabel($topic, "[".$topic->key."]adviser_validation") . "\n    ";
		
		$optscb = array('value' => ProjectTopic::YES, 'uncheckValue' => ProjectTopic::NO, 'class' => 'validation_cb owner_validation');
		if ($topic->owner_validation == ProjectTopic::INHERITS) {
			$optscb['value'] = ProjectTopic::INHERITS;
			$optscb['disabled'] = 'disabled';
		}
		echo CHtml::activeCheckBox($topic, "[".$topic->key."]owner_validation", $optscb);
		echo CHtml::activeLabel($topic, "[".$topic->key."]owner_validation") . "\n    ";
		echo "</div>";
		
		$opts = array();
		$opts['rows'] = '5';
		$opts['cols'] = '75';
		
		echo "<div id=\"validation_comment_" . $topic->key . "\" class=\"validation_area\">";
		echo "<b>" . CHtml::activeLabel($topic, "[".$topic->key."]validation_comment") . "</b><br />\n    ";
		echo CHtml::activeTextArea($topic, "[".$topic->key."]validation_comment", $opts) . "\n    ";
		echo "</div>";
		
		echo "<div><a name=\"objective_comment_".$topic->key."_lnk\" alt=\"commentaire d'objectifs\" class=\"objective_link\"></a>";
		$optscb = array('value' => ProjectTopic::YES, 'uncheckValue' => ProjectTopic::NO, 'class' => 'objective_cb objective');
		if ($topic->objective == ProjectTopic::INHERITS) {
			$optscb['value'] = ProjectTopic::INHERITS;
			$optscb['disabled'] = 'disabled';
		}
		echo CHtml::activeCheckBox($topic, "[".$topic->key."]objective", $optscb);
		echo CHtml::activeLabel($topic, "[".$topic->key."]objective") . "\n    ";
		echo "</div>";
		//		echo "(<a onclick=\"$('#objective_comments_" . $topic->key ."').dialog('open');\">comment</a>)";
		
/*$page->beginWidget('zii.widgets.jui.CJuiDialog', array(
    'id'=>"objective_comments_" . $topic->key,
		'htmlOptions' => array ('class'=>"objective_area"),
    // additional javascript options for the dialog plugin
    'options'=>array(
        'title'=>'Dialog box 1',
        'autoOpen'=>false,
    ),
));*/
		echo "    <div id=\"objective_comments_" . $topic->key . "\" class=\"objective_area\">\n      ";
		echo "<b>" . CHtml::activeLabel($topic, "[".$topic->key."]objective_needs") . "</b><br />\n      ";
		echo CHtml::activeTextArea($topic, "[".$topic->key."]objective_needs", $opts) . "<br />\n      ";
		
		echo "<b>" . CHtml::activeLabel($topic, "[".$topic->key."]objective_contacts") . "</b><br />\n      ";
		echo CHtml::activeTextArea($topic, "[".$topic->key."]objective_contacts", $opts) . "\n    ";
		echo "</div>";
		//$page->endWidget('zii.widgets.jui.CJuiDialog');
		
		echo "  </div>\n";
		echo "</div>\n";

		echo CHtml::errorSummary($topic);

		//global $iiii;
		//$iiii++;
		//if ($iiii <= 1)
		display_topics($page, $model, !$odd, $topic->children);
		//$iiii--;
		
		echo "</li>\n";
		$odd = !$odd;
	}
	echo "</ul>";
	return $odd;
}


//global $iiii;
//$iiii = 0;
echo "<div id=\"topics_tree\">";
display_topics($this, $model, true, $roots);
echo "</div>";
?>


<div class="action">
<?php echo CHtml::submitButton(Yii::t('views', 'Save')); ?>
</div>

<?php echo CHtml::endForm(); ?>

</div>


<?php
// Javascript

$cs = Yii::app()->clientScript;

$all = array();
$roots = array();
foreach($topics as $item) {
	$json = array();
	$json['key'] = $item->key;
	$children = array();
	foreach($item->children as $tmp) {
		$children[] = $tmp->key;
	}
	$json['children'] = $children;
	$json['parent'] = $item->parent_key;
	$all[$json['key']] = $json;
	if ($item->parent_key == null) {
		$roots[] = $json['key'];
	}
}


$attribute = '';
$opts = array();
CHtml::resolveNameId(new ProjectTopic(), $attribute, $opts);

$timeOutSaveInBg = 10*60*1000; // 10 minutes 

$script = <<<EOF
$(".summary").find('a').each(function (index, item) {
	var id = $(item).attr('href');
	$(item).removeAttr('href');
	$(item).click(function(){
		Item.steps.hide();
		if (id == '#all') {
			Item.steps.show();
		} else {
			$(id).show();
		}
	});
});

EOF;

$script .= "Item.init(\"".$opts['id'].'_'."\", \"" . Constants::JUI_DATE_FORMAT . "\", \"topics_tree\", ".CJavaScript::encode( array(
				"height"=>"200px",
				"width"=>"95%",
				"toolbar"=> Constants::ckEditorToolbars(),
			)).");\n";
$script .= "\nsetTimeout('saveInBackground()', $timeOutSaveInBg);";
$script .= "\nItem.steps.hide();$(Item.steps[0]).show()";
$saveInBackgroundUrl = $this->createUrl('saveInBackground');
$script2 = <<<EOF

function saveInBackground() {
	var url = '$saveInBackgroundUrl';
	Item.sendInBackground(url);
	setTimeout('saveInBackground()', $timeOutSaveInBg);
}

EOF;

$cs->registerScript('id', $script, CClientScript::POS_READY);
$cs->registerScript('loop', $script2, CClientScript::POS_HEAD);
$cs->registerScriptFile("/ckeditor/ckeditor.js");
$cs->registerScriptFile("/ckeditor/adapters/jquery.js");
$cs->registerScriptFile("/js/Exception.js");
$cs->registerScriptFile("/js/DateUtils.js");
$cs->registerScriptFile("/js/StringUtils.js");
$cs->registerScriptFile("/js/Logger.js");
$cs->registerScriptFile("/js/Item.js");
$cs->registerScriptFile("/js/json.js", CClientScript::POS_END);
$cs->registerCssFile('/css/steps.css');

?>
</div>
<?php 
